var gulp = require('gulp');
var shell = require('gulp-shell');

// [Neo] TODO change here
var project_name = 'wenwuju';
// [Neo] refer to `settings.gradle`
var module_name = 'wenwuju';
// [Neo] static file from local
var static_web_path = '-';
// [Neo] static file to http server
var static_web_name = '-';

var docker_web_name = project_name + '_web_1';
var docker_db_name = project_name + '_db_1';

var docker_project_volume = '/srv/docker/' + project_name;
var docker_web_path = docker_project_volume + '/web';
var docker_db_path = docker_project_volume + '/db';

// var security_java_file = '/etc/java-8-openjdk/security/java.security';

var nginx_server_conf_path = '/etc/nginx/default.d';
var nginx_reload_cmd = 'sudo systemctl reload nginx';
var nginx_root = '/srv/http';
var nginx_html_path = '/usr/share/nginx/html';

var db_in_docker = true;
var db_name = module_name;
var db_user = module_name;
// [Neo] TODO check here
var db_root_password = 'root';

gulp.task('default', shell.task([
  'if [ ! -e ' + nginx_root + ' ]; then sudo ln -s ' + nginx_html_path + ' ' + nginx_root + '; else if [ ! -L ' + nginx_root + ' ]; then echo ' + nginx_root + ' check failed!; exit 1; fi; fi',
  'c=`cat templates/.env` && t=`cat templates/docker-compose.template` && echo "$c\ncat<<EOF\n$t\nEOF" | bash > docker-compose.yml',
  'docker-compose up -d',
]));

gulp.task('stop', shell.task([
  'docker-compose stop',
]));

gulp.task('start', shell.task([
  'docker-compose start',
]));

gulp.task('clean', shell.task([
  'docker-compose down',
  'sudo rm -rf ' + docker_project_volume,
  'sudo rm -rf ' + nginx_server_conf_path + '/' + module_name + '.*',
]));

gulp.task('bye', ['clean'], shell.task([
  'sudo rm -rf .git src build node_modules templates backend web *.{sql,yml,md,json,js,gradle,conf,https}',
]));


gulp.task('restore', shell.task([
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysql -uroot -p' + db_root_password + ' --default-character-set=utf8mb4 < templates/db.sql',
]));

gulp.task('restore-rows', shell.task([
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysql -uroot -p' + db_root_password + ' --default-character-set=utf8mb4 < templates/db.rows.sql',
]));

gulp.task('dump', shell.task([
  'echo "--" > db.sql',
  'echo "-- DO NOT EDIT THIS FILE!" >> db.sql',
  'echo "-- ----------------------" >> db.sql',
  'echo "--" >> db.sql',
  'echo "" >> db.sql',
  'echo "DROP DATABASE IF EXISTS ' + db_name + ';" >> db.sql',
  'echo "CREATE DATABASE ' + db_name + ';" >> db.sql',
  'echo "GRANT ALL PRIVILEGES ON ' + db_name + '.* TO ' + db_user + '@\'%\' IDENTIFIED BY \'' + db_user + '.docker\';" >> db.sql',
  'echo "USE ' + db_name + ';" >> db.sql',
  'echo "" >> db.sql',
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysqldump -uroot -p' + db_root_password + ' ' + db_name + ' -d >> db.sql',
]));

gulp.task('dump-rows', shell.task([
  'echo "--" > db.rows.sql',
  'echo "-- DO NOT EDIT THIS FILE!" >> db.rows.sql',
  'echo "-- ----------------------" >> db.rows.sql',
  'echo "--" >> db.rows.sql',
  'echo "" >> db.rows.sql',
  'echo "DROP DATABASE IF EXISTS ' + db_name + ';" >> db.rows.sql',
  'echo "CREATE DATABASE ' + db_name + ';" >> db.rows.sql',
  'echo "GRANT ALL PRIVILEGES ON ' + db_name + '.* TO ' + db_user + '@\'%\' IDENTIFIED BY \'' + db_user + '.docker\';" >> db.rows.sql',
  'echo "USE ' + db_name + ';" >> db.rows.sql',
  'echo "" >> db.rows.sql',
  (db_in_docker ? 'docker exec -i ' + docker_db_name : '') +
    ' mysqldump -uroot -p' + db_root_password + ' ' + db_name + ' >> db.rows.sql',
]));

gulp.task('compile', shell.task([
  'gradle clean war',
]));

gulp.task('deploy', ['compile'], shell.task([
  'if [ -d "' + static_web_path + '" ]; then sudo rm -rf ' + nginx_root + '/' + static_web_name + '; sudo cp -R ' + static_web_path + ' ' + nginx_root + '/' + static_web_name +'; sudo find ' + nginx_root + '/' + static_web_name + ' -type d -exec sudo chmod +rx {} \\;; fi',
  'pn='+project_name+' && c=`cat templates/.env` && t=`cat templates/nginx.template` && echo "PROJECT_NAME=$pn\n$c\ncat<<EOF\n$t\nEOF" | bash > $pn.conf',
  'pn='+project_name+' && c=`cat templates/.env` && t=`cat templates/nginx.https.template` && echo "PROJECT_NAME=$pn\n$c\ncat<<EOF\n$t\nEOF" | bash > $pn.conf.https',
  'pn='+project_name+' && c=`cat templates/.env` && t=`cat templates/nginx.file.template` && echo "PROJECT_NAME=$pn\n$c\ncat<<EOF\n$t\nEOF" | bash > "$pn"uploaded.conf',
  'sudo cp *.conf* ' + nginx_server_conf_path + '',
  ' ' + nginx_reload_cmd + ' ',
  'docker stop ' + docker_web_name,
  'sudo mv build/libs/*.war ' + docker_web_path,
  'docker start ' + docker_web_name
]));

